/**
* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/

/* !
 * \file aicore_kernel.asc
 * \brief
 */

#include "kernel_operator.h"
#include "kernel_args.h"

template<typename T, int8_t mode, int8_t len, bool super>
__aicore__ void hello_world_impl(GM_ADDR m)
{
    if constexpr (std::is_same_v<T, float>){
        AscendC::printf("Hello World: float mode %u len %u.\n", mode, len);
    } else if constexpr (std::is_same_v<T, int>) {
        AscendC::printf("Hello World: int mode %u len %u.\n", mode, len);
    }
}

template<typename T, int8_t mode, int8_t len, bool super>
__global__ __aicore__ void hello_world(GM_ADDR m, GM_ADDR TilingPtr)
{
    KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_2);
    if constexpr (!super) {
        hello_world_impl<T, mode, len, false>(m);
    } else {
        __gm__ struct KernelInfo::TilingInfo *ti = (__gm__ struct KernelInfo::TilingInfo *)TilingPtr;
        if ASCEND_IS_AIV {
            if (AscendC::GetBlockIdx() == 0) {
                volatile __gm__ uint64_t *lock = &ti->lock;
                while (*lock == 0) {
                    dcci(lock, cache_line_t::SINGLE_CACHE_LINE, dcci_dst_t::CACHELINE_OUT);
                }
            }
        }
        AscendC::SyncAll<false>();
        
        if (ti->type ==0 && ti->mode == 1 && ti->len == 2) {
            hello_world_impl<float, 1, 2, false>(m);
        } else if (ti->type == 1 && ti->mode == 2 && ti->len == 4) {
            hello_world_impl<int, 2, 4, false>(m);
        }
        if ASCEND_IS_AIV {
            if (AscendC::GetBlockIdx() == 0) {
                volatile __gm__ uint64_t *lock = &ti->lock;
                *lock = 0;
                dcci(lock, cache_line_t::SINGLE_CACHE_LINE, dcci_dst_t::CACHELINE_OUT);
            }
        }
    }
}

extern "C" void hello_world_do(uint32_t blockDim, void *stream, uint8_t*m, uint8_t *ti)
{
    hello_world<int, 10, 20, true><<<1, nullptr, stream>>>(m, ti);
}